package com.villaday.idf.activity;


import android.content.Intent;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.adapter.CustomerOrderAdapterFor3;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.model.VipsMemberOrders;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * 客户订单
 */
public class CustomerOrderActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, AbsRecyclerViewAdapter.OnItemClickListener, OnAdapterCallbackListener, OkHttpUtil.OnDataListener {

    @BindView(R.id.toolbar_icon)
    Toolbar toolbar;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    Unbinder unbinder;
    private LoadingDialog loadingDialog;
    private Gson gson = new GsonBuilder().create();

    private int pageNo = 1;
    private int nextIndex = 1;
    private int pageSize = 20;
    List<VipsMemberOrders.BodyBean.ListBean> data = new ArrayList();
    private CustomerOrderAdapterFor3 customerorderAdapter;
    String vipOrderType;


    @Override
    protected int getViewResId() {
        return R.layout.activity_customer_order;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbar, "客户订单", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        }, R.drawable.icon_search, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(CustomerOrderActivity.this, SearchCustomerOrderActivity.class).putExtra("vipOrderType", getIntent().getStringExtra("vipOrderType")));
            }
        });
        vipOrderType = getIntent().getStringExtra("vipOrderType");
        srl.setOnRefreshListener(this);
        rv.setLayoutManager(new LinearLayoutManager(this));
        customerorderAdapter = new CustomerOrderAdapterFor3(this, this);
        rv.setAdapter(customerorderAdapter);

        //rv.addItemDecoration(new RVItemDecoration(LinearLayout.VERTICAL, PhoneUtil.dp2px(this, 1), getResources().getColor(R.color.colorBg), PhoneUtil.dp2px(this, 8)));
        customerorderAdapter.setOnItemClickListener(this);
 /*       View inflate = View.inflate(this, R.layout.item_head_income_details, null);
        customerListAdapter.addHeaderView(inflate);*/
        customerorderAdapter.setEmptyView(R.layout.empty_tips);
        customerorderAdapter.setEmptyTips(R.id.tv_emptyTips, "没有更多了");
    }

    @Override
    protected void loadData() {
        getData();
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", pageNo + "");
        map.put("pageSize", pageSize + "");
        map.put("vipOrderType", vipOrderType);
        OkHttpUtil.getJSON(this, Constant.URL.vips_member_orders, map, this);
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        nextIndex = 1;
        getData();
    }

    //rv item 点击事件
    @Override
    public void onItemClick(View v, int position) {
        switch (v.getId()) {
            case R.id.tv_into_goods: {
                if (data.get(position).getType() == 0) { //普通房源
                    Intent intent = new Intent(this, HouseDetailsActivityFor3.class);
                    intent.putExtra("houseId", data.get(position).getProduct().getId());
                    intent.putExtra("persellId", "");
//                    intent.putExtra("type", data.get(position).getType());
                    startActivity(intent);

                } else if (data.get(position).getType() == 7) { //特卖房源
                    Intent intent = new Intent(this, GoodsAndFodderActivityFor3.class);
                    intent.putExtra("houseId", data.get(position).getProduct().getId());
                    intent.putExtra("persellId", data.get(position).getActivity().getId());
//                    intent.putExtra("type", data.get(position).getType());
                    startActivity(intent);
                }
            }
            break;
        }

    }


    @Override
    public void onCallback() {
        if (nextIndex == pageNo + 1) {
            pageNo++;
            getData();
        }
    }

    @Override
    public void onResponse(String url, String json) {
        if (Constant.URL.vips_member_orders.equals(url)) {
            LogeUtil.e("vips_member_orders", json);
            dismissLoading();

            VipsMemberOrders vipsMemberOrders = gson.fromJson(json, VipsMemberOrders.class);
            removeLoadingItem();
            if (pageNo == 1) {
                data.clear();
            }

            if ("200".equals(vipsMemberOrders.getCode())) {
                   /* if (pageNo == 1 && presellEntity.getBody().getRecomendList() != null && presellEntity.getBody().getRecomendList().getList() != null) {

                        dataHead.addAll(presellEntity.getBody().getRecomendList().getList());
                        homeListSaleHeadAdapter.setData(dataHead);
                    }*/
                data.addAll(vipsMemberOrders.getBody().getList());
                if (vipsMemberOrders.getBody().isHasNext()) {
                    data.add(new VipsMemberOrders.BodyBean.ListBean(1));
                    nextIndex = pageNo + 1;

                } else {
                    addBaseLine();
                }

            } else {
                addBaseLine();
            }
            customerorderAdapter.setData(data);
        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    private void dismissLoading() {

        if (srl != null && srl.isRefreshing()) {
            srl.setRefreshing(false);
        }

    }

    private void removeLoadingItem() {
        if (data.size() > 0) {
            if (data.get(data.size() - 1).getMyType() == 1) {
                data.remove(data.size() - 1);
            }
        }
    }

    private void addBaseLine() {
        if (pageNo != 1) {
            data.add(new VipsMemberOrders.BodyBean.ListBean(2));
        }
    }

}
